﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Forms;

namespace MonitorManager
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private ObservableCollection<ListBindData> bindItems = new ObservableCollection<ListBindData>();// 绑定的资源元素集合

        private List<ObservableCollection<ListBindData>> mediaList = new List<ObservableCollection<ListBindData>>();//媒体资源列表集合

        private BackgroundWorker backWorker = new BackgroundWorker();

        private List<Window> playWndList = new List<Window>();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //初始化媒体列表
            for (int i = 0; i < Screen.AllScreens.Length; i++)
            {
                ObservableCollection<ListBindData> item = new ObservableCollection<ListBindData>();
                mediaList.Add(item);
            }
            //初始化屏幕列表
            MonitorList.ItemsSource = bindItems;
            int monitorCount = 1;
            int LastScreenWidth = 0;
            foreach (Screen s in Screen.AllScreens)
            {
                Rectangle ScreenArea = s.Bounds;
                using (Bitmap bmp = new Bitmap(ScreenArea.Width, ScreenArea.Height))
                {
                    using (Graphics g = Graphics.FromImage(bmp))
                    {
                        g.CopyFromScreen(LastScreenWidth, 0, 0, 0, new System.Drawing.Size(ScreenArea.Width, ScreenArea.Height));
                    }
                    LastScreenWidth += ScreenArea.Width;
                    ListBindData bindData = new ListBindData();
                    bindData.StrethMethod = System.Windows.Media.Stretch.UniformToFill;
                    bindData.ItemPic = ImageTool.ChangeBitmapToBitmapSource(bmp);
                    bindData.ItemWidth = 160;
                    bindData.ItemHeight = 90;
                    bindData.ItemName = "显示器" + monitorCount++;
                    bindItems.Add(bindData);

                    bmp.Dispose();
                }
            }

            //开始更新图片线程，间隔500ms
            backWorker.DoWork += BackWorker_DoWork;
            backWorker.WorkerSupportsCancellation = true;
            backWorker.RunWorkerAsync();
            
        }

        private void BackWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            while (!e.Cancel)
            {
                CaptureAllMonitor();
                Thread.Sleep(500);
            }
        }

        private void Timer_Tick(object sender, System.EventArgs e)
        {
            CaptureAllMonitor();
        }

        private void CaptureAllMonitor()
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                int LastScreenWidth = 0;
                //获取整个屏幕图像,不包括任务栏
                for (int i = 0; i < Screen.AllScreens.Length; i++)
                {
                    Screen s = Screen.AllScreens[i];
                    Rectangle ScreenArea = s.Bounds;

                    using (Bitmap bmp = new Bitmap(ScreenArea.Width, ScreenArea.Height))
                    {
                        using (Graphics g = Graphics.FromImage(bmp))
                        {
                            g.CopyFromScreen(LastScreenWidth, 0, 0, 0, new System.Drawing.Size(ScreenArea.Width, ScreenArea.Height));
                        }
                        LastScreenWidth += ScreenArea.Width;

                        bindItems[i].ItemPic = ImageTool.ChangeBitmapToBitmapSource(bmp);
                        
                        bmp.Dispose();
                        GC.Collect();
                    }
                }
            }));
        }

        private void btnStartAll_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < Screen.AllScreens.Length; i++)
            {
                Screen s = Screen.AllScreens[i];
                if (mediaList[i].Count > 0)
                {
                    PlayerWindow playerWnd = new PlayerWindow(mediaList[i][0].ItemPath);
                    playerWnd.Left = s.Bounds.X + 1;
                    playerWnd.Top = s.Bounds.Y + 1;
                    playWndList.Add(playerWnd);
                    playerWnd.Show();
                    playerWnd.WindowState = WindowState.Maximized;
                }
                //Thread.Sleep(1000);
            }
        }

        private void btnCloseAll_Click(object sender, RoutedEventArgs e)
        {
            foreach(Window w in playWndList)
            {
                w.Close();
            }
        }

        private void btnAddFile_Click(object sender, RoutedEventArgs e)
        {
           if( PlayingList.ItemsSource != null)
            {
                OpenFileDialog ofdlg = new OpenFileDialog();
                ofdlg.Filter = "所有支持的文件(*.mp4)|*.mp4";
                if(ofdlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    ObservableCollection<ListBindData> items = (ObservableCollection<ListBindData>)PlayingList.ItemsSource;
                    ListBindData item = new ListBindData();
                    item.ItemPath = ofdlg.FileName;
                    item.ItemName = Path.GetFileName(ofdlg.FileName);
                    item.ItemPic = ImageTool.GetThumbnailByPath(ofdlg.FileName);
                    item.ItemWidth = 160;
                    item.ItemHeight = 90;
                    items.Add(item);


                }
              
            }
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            if(MonitorList.SelectedIndex >= 0 && PlayingList.SelectedIndex >= 0)
            {
                if (System.Windows.MessageBox.Show("确认删除所选媒体文件吗？", "删除所选", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    mediaList[MonitorList.SelectedIndex].RemoveAt(PlayingList.SelectedIndex);
                }
            }
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            if (MonitorList.SelectedIndex >= 0)
                if (System.Windows.MessageBox.Show("确认要清除所有媒体文件吗？(文件本身不会删除）", "清空所有", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    mediaList[MonitorList.SelectedIndex].Clear();
                }
        }

        private void MonitorList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (MonitorList.SelectedIndex >= 0)
                PlayingList.ItemsSource = mediaList[MonitorList.SelectedIndex];
        }

        private void btnSaveAs_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog sfDlg = new SaveFileDialog();
            sfDlg.Filter = "配置文件(*.config)|*.config";
            sfDlg.FileName = "方案1";
            if(sfDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

            }
        }

        private void btnLoadConfig_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofdlg = new OpenFileDialog();
            ofdlg.Filter = "配置文件(*.config)|*.config";
            if (ofdlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
            }
        }
    }
}
